package xj.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observer;


public class DrawToolkit {

  protected List<DrawTool> tools;

  protected Map<String, Integer> toolmap;

  protected int curTool;

  protected int lastTool;

  public DrawToolkit() {
    this(16);
  }

  public DrawToolkit(int maxTool) {
    tools = new ArrayList<DrawTool>(maxTool);
    lastTool = curTool = -1;
  }

  /**
   * Add a new tool to the tool kit. Return the index of the new tool.
   */
  public int addTool(DrawTool tool) {
    if (tool != null) {
      tools.add(tool);
      return (tools.size() - 1);
    }
    return -1;
  }

  public int addTool(String name, DrawTool tool) {
    if (tool != null) {
      int t = addTool(tool);
      if (t >= 0 && name != null) {
	if (toolmap == null) {
	  toolmap = new HashMap<String, Integer>();
	}
	toolmap.put(name, new Integer(t));

	if (debug) {
	  System.out.println("DrawToolkit.addTool(): " + name + " @" + t);
	}

      }
      return t;
    }
    return -1;
  }

  public int getToolCount() {
    return tools.size();
  }

  public void setCurrentTool(String name, Observer o) {
    if (name != null) {
      Object t = toolmap.get(name);
      if (t instanceof Integer) {
	setCurrentTool(((Integer) t).intValue(), o);
      }
    }
  }

  public void setCurrentTool(DrawTool tool, Observer o) {
    int t = findTool(tool);
    if (t >= 0) {
      setCurrentTool(t, o);
    }
  }

  public void setCurrentTool(int t, Observer o) {
    //System.out.println("DrawToolkit.setCurrentTool()");
    int numTool = tools.size(); 
    if (curTool >= 0 && curTool < numTool) {
      tools.get(curTool).deactivate(o);
    }
    if (numTool > 0) {
      lastTool = curTool;
      if (t >= 0 && t < numTool) {
	curTool = t;
      } else {
	curTool = 0;
      }
      tools.get(curTool).activate(o);
    }
  }

  public DrawTool getCurrentTool() {
    int numTool = tools.size(); 
    if (numTool > 0) {
      if (curTool >= 0 && curTool < numTool) {
	return tools.get(curTool);
      } else {
	return tools.get(0);
      }
    } else {
      return null;
    }
  }

  public DrawTool getLastTool() {
    int numTool = tools.size(); 
    if (numTool > 0 && 
	lastTool >= 0 && 
	lastTool < numTool) {
      return tools.get(lastTool);
    } else {
      return null;
    }
  }

  public DrawTool getTool(int t) {
    int numTool = tools.size(); 
    if (t >= 0 && t < numTool) {
      return tools.get(t);
    }
    return null;
  }

  public DrawTool getTool(String name) {
    if (name != null) {
      int numTool = tools.size(); 
      Object t = toolmap.get(name);
      if (t instanceof Integer) {
	int t1 = ((Integer) t).intValue();
	if (t1 >= 0 && t1 < numTool) {
	  return tools.get(t1);
	}
      }
    }
    return null;
  }

  protected int findTool(DrawTool tool) {
    if (tool != null) {
      int numTool = tools.size(); 
      for (int i = 0; i < numTool; i++) {
	if (tools.get(i) == tool) {
	  return i;
	}
      }
    }
    return -1;
  }

  protected static final boolean debug = false;

}
